from django.core.cache import cache
from groupMembers.models import group_mem_assoc_v
from utils import common_methods

# This view is used to store user, member, startgroup, member_group
# data at centralized location
def get_group_member(userid, forcequery):
    
    if forcequery == False:
        # Check if data exists in memcache  
        mem_grp_data = cache.get('mem_grp_data_key' +userid)
    else:
        mem_grp_data = None
        cache.delete('mem_grp_data_key' + userid)
    
    if mem_grp_data is None:
        # Get data
        mem_grp_data = group_mem_assoc_v.objects.filter(userid=userid)
        # Store in cache. Django suggest that we need to use both
        # set() and add() method. add() is used to add key to  cache
        # if its not already present.
        cache.set('mem_grp_data_key' + userid, mem_grp_data, 24*3600)
        cache.add('mem_grp_data_key' + userid, mem_grp_data, 24*3600)
        
    return mem_grp_data

# This view is used to store user, member, startgroup, member_group
# data at centralized location
def get_member_from_gwebaddr(web_address, forcequery):
    
    if forcequery == False:
        # Check if data exists in memcache  
        grp_members_data = cache.get('grp_members_data_key' +web_address)
    else:
        grp_members_data = None
        cache.delete('grp_members_data_key' + web_address)
        
    if grp_members_data is None:
        # Get data
        grp_members_data = group_mem_assoc_v.objects.filter(group_web_address=web_address).order_by('-join_date')
        # Store in cache. Django suggest that we need to use both
        # set() and add() method. add() is used to add key to  cache
        # if its not already present.
        cache.set('grp_members_data_key' + web_address, grp_members_data, 24*3600)
        cache.add('grp_members_data_key' + web_address, grp_members_data, 24*3600)
        
    return grp_members_data

# This view is used to store user's group permission
# data in cache
def get_user_group_permission(userid, group_web_address, forcequery):
    
    if forcequery == False:
        # Check if data exists in memcache  
        permission = cache.get('usr_grp_perm_key' +userid + group_web_address)
    else:
        permission = None
        cache.delete('usr_grp_perm_key' +userid + group_web_address)
    
    if permission is None:
        # Get Permission
        # Prepare sub list of group members
        mem_grp_data = get_member_from_gwebaddr(group_web_address, False)
        
        # Get User object with permissions assigned
        perm_level = common_methods.getUserPermission(mem_grp_data, userid, group_web_address)
    
        # Get the user permission string. A complete list could have been sent on client side
        # But for the security reason and faster process, send minimal information
        # needed on client    
        permission = common_methods.getPermissionString(perm_level)
    
        # Store in cache. Django suggest that we need to use both
        # set() and add() method. add() is used to add key to  cache
        # if its not already present.
        cache.set('usr_grp_perm_key' +userid + group_web_address, permission, 24*3600)
        cache.add('usr_grp_perm_key' +userid + group_web_address, permission, 24*3600)
        
    return permission

# Get Email addresses of group users whose email flag is active
def getGroupMembersEmailIds(group_web_addr, email_setting_flag):
    email_setting_filter = email_setting_flag +"1;"
    # Only pull out users whose email_settings_flag is set
    return group_mem_assoc_v.objects.filter(group_web_address = group_web_addr, email_setting__icontains = email_setting_filter).values_list('email', flat=True)